import wheels.pi.aio as aio

from wheels.pi.aio.AsyncOperation import AcceptOperation
from wheels.pi.aio.Proactor import EventHandler


class AsyncAcceptor(object):

    class _ProactiveHandler(EventHandler):

        def __init__(self, handler, act):
            self._handler = handler
            self._act = act

        def handle_accept(self, client_socket, remote_address):
            value = (client_socket, remote_address)
            self._handler.handle_event("accept", value, self._act)


    def __init__(self, handle, proactor, handler):
        self._handle = handle
        self._proactor = proactor
        self._handler = handler


    def accept(self, act = None):
        op = AcceptOperation(self._handle)
        proactive = AsyncAcceptor._ProactiveHandler(self._handler, act)
        aio.register(op, self._proactor, proactive)
